package com.leo.lime.music.content.me.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.leo.lime.music.R;
import com.leo.lime.music.controller.MusicController;
import com.leo.lime.music.content.me.domain.UserInfo;

public class UserInfoFragment extends Fragment {
    private static final String TAG = "UserInfoFragment";

    private ImageView mUserIconIv;
    private TextView mUserNameTv;
    private TextView mUserDetailTv;
    private ImageView mUserEntranceIv;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(getUserInfoLayout(), container, false);
    }

    private int getUserInfoLayout() {
        return R.layout.content_me_user_info;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUserIconIv = view.findViewById(R.id.user_icon);
        mUserNameTv = view.findViewById(R.id.user_info_name);
        mUserDetailTv = view.findViewById(R.id.user_info_intro);
        mUserEntranceIv = view.findViewById(R.id.user_info_entrance);

        updateUserInfoUi();
    }

    private void updateUserInfoUi() {
        UserInfo info = MusicController.getInstance(getActivity()).getUserInfo();
        mUserIconIv.setBackground(getUserIconDrawable(info));
        mUserNameTv.setText(getUserNameText(info));
        mUserDetailTv.setText(getUserDetailText(info));
        mUserEntranceIv.setBackground(getUserDetailDrawable(info));
    }

    private Drawable getUserIconDrawable(UserInfo info) {
        if(info == null) {
            return getResources().getDrawable(R.drawable.ic_user);
        }
        return info.getUserIcon();
    }

    private CharSequence getUserNameText(UserInfo info) {
        if(info == null) {
            return getResources().getText(R.string.default_user_name);
        }
        return info.getUserName();
    }

    private CharSequence getUserDetailText(UserInfo info) {
        if(info == null) {
            return getResources().getText(R.string.default_user_detail);
        }
        return info.getUserDetail();
    }

    private Drawable getUserDetailDrawable(UserInfo info) {
        if(info == null) {
            return getResources().getDrawable(R.drawable.ic_messaging);
        }
        return getResources().getDrawable(R.drawable.ic_messaging);
    }
}
